import {pageModalTip as pagetip, filterIp, filterPort} from '/js/usemain.js';
import {default as BeanComputer} from '../js/bean/BeanComputer.js'

'use strict'

let dataarea = $('#computer').children('div:eq(1)').children('table').children('tbody');
let thispagerow = 20;
let modifyComputer = new BeanComputer();
let modifyline = -1;
//==========================================================================
//
//==========================================================================

function computerInitFillTag() {
	let html='';
	for (let i = 0; i < thispagerow; i++) {
		html += '<tr class="row">'
			+ '<td class="col d-none"></td>'
			+ '<td class="col">'
			+  '<div class="pl-3"></div>'
			+ '</td>'
			+ '<td class="col">'
			+  '<div class="pl-3"></div>'
			+ '</td>'
			+ '<td class="col">'
			+  '<div class="pl-3"></div>'
			+ '</td>'
			+ '<td class="col text-left">'
			+  '<button type="button" class="btn btn-danger ml-3 edit-computer">修改</button>'
			+  '<button type="button" class="btn btn-danger ml-3 delete-computer">删除</button>'
			+ '</td>'
			+ '</tr>';
	}
	html += '<tr class="row table-success" id="addcomputer">'
		+ '<td class="col d-none"></td>'
		+ '<td class="col">'
		+  '<input type="text" class="ipcheck ml-3" placeholder="">'
		+ '</td>'
		+ '<td class="col">'
		+  '<input type="text" class="ipcheck ml-3" placeholder="">'
		+ '</td>'
		+ '<td class="col">'
		+  '<input type="text" class="ipcheck ml-3" placeholder="">'
		+ '</td>'
		+ '<td class="col text-left">'
		+  '<button type="button" class="btn btn-primary ml-3 save-add-computer">保存</button>'
		+  '<button type="button" class="btn btn-info ml-3 cancel-add-computer">取消</button>'
		+ '</td>'
		+ '</tr>';
	dataarea.html(html);
	dataarea.children().hide();
}

function computerClearDataOfRow(i) {
	if(i >= 0) {
		dataarea.children('tr:eq('+i+')').children('td:eq(0)').html('');
		dataarea.children('tr:eq('+i+')').children('td:eq(1)').children('div').html('');
		dataarea.children('tr:eq('+i+')').hide();
	}
}


function computerClearAddTag(){
	$('#addcomputer').children('td:eq(0)').html();
	$('#addcomputer').children('td:eq(1)').children('input').val(null);
	$('#addcomputer').children('td:eq(2)').children('input').val(null);
	$('#addcomputer').children('td:eq(3)').children('input').val(null);
	$('#addcomputer').hide();
}

function computerRowToEdit(){
	modifyComputer.msg();
	
	let i = modifyline;
	$('#computer').children('div:eq(0)').children('div:eq(1)').hide(); // 表外部的新增按钮隐藏
	// 表区域
	$('.delete-computer').hide();
	$('.edit-computer').hide();
	if (i>=0) {
		let rowhtml = dataarea.children('tr:eq('+i+')');
		rowhtml.children('td:eq(1)').html('<input type="text" class="ipcheck ml-3" value="'+modifyComputer.cName+'">');
		rowhtml.children('td:eq(2)').html('<input type="text" class="ipcheck ml-3" value="'+modifyComputer.tag+'">');
		rowhtml.children('td:eq(3)').html('<input type="text" class="ipcheck ml-3" value="'+modifyComputer.ip+'">');
		rowhtml.children('td:eq(4)').append('<button type="button" class="btn btn-primary ml-3 save-computer">保存</button>'
				+  '<button type="button" class="btn btn-info ml-3 cancel-add-computer">取消</button>');
		rowhtml.children('td:eq(4)').children('button:eq(2)').click(computerEdit);
		rowhtml.children('td:eq(4)').children('button:eq(3)').click(computerCancelEdit);
	}
}
function computerRowRecover(){
	$('#computer').children('div:eq(0)').children('div:eq(1)').show();
	$('.delete-computer').show();
	$('.edit-computer').show();
	let i = modifyline;
	modifyline = -1;
	if (i>=0) {
		let rowhtml = dataarea.children('tr:eq('+i+')');
		rowhtml.children('td:eq(4)').children('button:eq(3)').remove();
		rowhtml.children('td:eq(4)').children('button:eq(2)').remove();
		rowhtml.children('td:eq(1)').html('<div class="pl-3"></div>');
		rowhtml.children('td:eq(2)').html('<div class="pl-3"></div>');
		rowhtml.children('td:eq(3)').html('<div class="pl-3"></div>');
	}
}

function computerRowEditRecover(){
	let i = modifyline;
	modifyline = -1;
	$('#computer').children('div:eq(0)').children('div:eq(1)').show();
	$('.delete-computer').show();
	$('.edit-computer').show();
	if (i>=0) {
		let rowhtml = dataarea.children('tr:eq('+i+')');
		rowhtml.children('td:eq(4)').children('button:eq(3)').remove();
		rowhtml.children('td:eq(4)').children('button:eq(2)').remove();
		rowhtml.children('td:eq(1)').html('<div class="pl-3">' + modifyComputer.cName+ '</div>');
		rowhtml.children('td:eq(2)').html('<div class="pl-3">' + modifyComputer.tag+ '</div>');
		rowhtml.children('td:eq(3)').html('<div class="pl-3">' + modifyComputer.ip + '</div>');
	}
}

function computerFillData(data) {
	let total = 0;
	if (data) {
		total = data.length > thispagerow ? thispagerow : data.length;
		$.each(eval(data), function(i, one) {
			if (i < total) {
				dataarea.children('tr:eq('+i+')').show();
				dataarea.children('tr:eq('+i+')').children('td:eq(0)').html(one.id);
				dataarea.children('tr:eq('+i+')').children('td:eq(1)').children('div').html(one.name);
				dataarea.children('tr:eq('+i+')').children('td:eq(2)').children('div').html(one.tag);
				dataarea.children('tr:eq('+i+')').children('td:eq(3)').children('div').html(one.ip);
				dataarea.children('tr:eq('+i+')').children('td:eq(4)').children().show();
			}
		});
	}
}

//==========================================================================
//前端向后端的请求
//==========================================================================
function computerAskSearch(){
	$.get('../ipother/getComputerList', function(result) {
		if (result.status>=0) {
			computerFillData(result.data);
		} else {
			if (result.tips && result.tips.length > 0) {
				pagetip('获取其他IP信息列表', result.tips);
			} else {
				pagetip('获取其他IP信息列表', '获取列表失败');
			}
		}
	});
}

function computerAskSave(pagetiphead, cid,cname,ctag,cip){
	$.get('../ipother/saveComputer', {
		"id" : cid,
		"name" : cname,
		"tag" : ctag,
		"ip" : cip
	}, function(result) {
		if (result.status>=0) {
			if (result.data) {
				pagetip(pagetiphead,'保存成功');
				computerSearch();
			} else {
				pagetip(pagetiphead,'保存失败');
			}
		} else {
			if (result.tips && result.tips.length > 0) {
				pagetip(pagetiphead,result.tips);
			} else {
				pagetip(pagetiphead,'保存失败');
			}
		}
	});
}

function computerAskDel(id){
	if (id && parseInt(id) > 0) {
		$.get('../ipother/delComputer', {
			'id' : parseInt(id)
		}, function(result) {
			console.info(result);
			if (result.status>=0) {
				if (result.data) {
					pagetip('删除其他IP信息', '删除成功');
					computerSearch();
				} else {
					pagetip('删除其他IP信息', '删除失败');
				}
			} else {
				if (result.tips && result.tips.length > 0) {
					pagetip('删除其他IP信息', result.tips);
				} else {
					pagetip('删除其他IP信息', '删除失败');
				}
			}
		});
	}
}

function myselfAskEdit(cid,cname,ctag,cip){
	computerAskSave(cid,cname,ctag,cip);
}
//==========================================================================
//界面按钮的事件
//==========================================================================
function computerSearch() {
	for (let i = 0; i < thispagerow; i++) {
		computerClearDataOfRow(i);
	}
	computerRowRecover();
	computerClearAddTag();
	computerAskSearch();
}
function computerDelete(){
	let rowtr = $(this).parent().parent();
//	let index = rowtr.index();
	let id = rowtr.children('td:eq(0)').html();
	computerAskDel(id);
}

function computerBeforeAdd() {
	$('#addcomputer').show();
	$('.delete-computer').hide();
	$('.edit-computer').hide();
}

function computerAdd() {
	let tiphead = '新增其他IP相关信息';
	let cname =  $('#addcomputer').children('td:eq(1)').children('input').val();
	if (null == cname || 0 == cname.trim().length) {
		pagetip(tiphead,'请设置主机名称');
	} else {
		cname = cname.trim();
		let ctag = $('#addcomputer').children('td:eq(2)').children('input').val();
		if (null == ctag) {
			ctag = '';
		}
		let cip = filterIp($('#addcomputer').children('td:eq(3)').children('input').val());
		if (cip) {
			computerAskSave(tiphead, null, cname, ctag, cip);
		} else {
			pagetip(tiphead,'IP（IPv4）不合法');
		}
	}
}

function computerCancelAdd() {
	computerClearAddTag();
	$('.delete-computer').show();
	$('.edit-computer').show();
}

function computerBeforeEdit(){
	modifyComputer.clear();
	let rowhtml = $(this).parent().parent();
	modifyline = rowhtml.index();
	modifyComputer.cId = parseInt(rowhtml.children('td:eq(0)').html().trim());
	modifyComputer.cName = rowhtml.children('td:eq(1)').children('div:eq(0)').html();
	modifyComputer.tag = rowhtml.children('td:eq(2)').children('div:eq(0)').html();
	modifyComputer.ip = rowhtml.children('td:eq(3)').children('div:eq(0)').html();
	computerRowToEdit();
}

function computerEdit(){
	if (null!=modifyComputer && $.isNumeric(modifyComputer.cId) && modifyComputer.cId > 0) {
		let rowhtml = $(this).parent().parent();
		let cname = rowhtml.children('td:eq(1)').children('input').val();
		let ctag =rowhtml.children('td:eq(2)').children('input').val();
		let cip =rowhtml.children('td:eq(3)').children('input').val();
		let havechange = false;
		let tiphead = '修改其他IP相关信息';
		// 主机名称必须有内容，空字符串时视为不修改
		if (null == cname || 0 == cname.trim().length) {
			cname = modifyComputer.cName;
		} else if (modifyComputer.cName == cname.trim()) {
		} else {
			havechange = true;
		}
		// 主机名称可空
		if (null == ctag) {
			ctag = modifyComputer.tag; // 执行不到
		} else if (modifyComputer.tag == ctag) {
		} else {
			havechange = true;
		}
		
		if (null == cip || 0 == cip.trim().length) {
			cip = modifyComputer.ip;
		} else if (modifyComputer.ip == cip.trim()) {
		} else {
			cip = filterIp(cip);
			if (null == cip) {
				pagetip(tiphead,'IP（IPv4）不合法');
				return;
			}
			// checkIp
			havechange = true;
		}
		if (havechange) {
			computerAskSave(tiphead, modifyComputer.cId, cname, ctag, cip);
		} else {
			pagetip(tiphead,'没有数据改变');
		}
	}
}

function computerCancelEdit(){
	computerRowEditRecover();
	modifyComputer.clear();
}
//==========================================================================
//初始化执行操作
//==========================================================================
// computerInitCSS();// 只执行一次
computerInitFillTag();// 只执行一次
computerSearch();
//==========================================================================
//给界面按钮绑定事件
//==========================================================================
$('#computer').children('div:eq(0)').children('div:eq(0)').click(computerSearch);
$('#computer').children('div:eq(0)').children('div:eq(1)').click(computerBeforeAdd);

$('.edit-computer').click(computerBeforeEdit);
$('.delete-computer').click(computerDelete);
$('.save-add-computer').click(computerAdd);
$('#addcomputer').children('td:eq(4)').children('button:eq(1)').click(computerCancelAdd);
//$('#addcomputer').children('td:eq(2)').children('button:eq(0)').click(computerSave);

